package com.prs.crm.action.fs;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.crm.Linkman;
import com.prs.crm.domain.fs.BankAccount;
import com.prs.crm.domain.fs.Outlay;
import com.prs.crm.domain.fs.OutlayDetail;
import com.prs.crm.domain.fs.OutlayItem;
import com.prs.crm.domain.oa.JobPlanDetail;
import com.prs.crm.domain.project.Project;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.crm.LinkmanService;
import com.prs.crm.service.fs.BankAccountService;
import com.prs.crm.service.fs.OutlayItemService;
import com.prs.crm.service.fs.OutlayService;
import com.prs.crm.service.project.ProjectService;

public class OutlayAction extends BaseAction {
	private Outlay outlay;
	private OutlayService outlayService;
	private PaginationSupport pagination;
	private Integer[] ids;

	private BankAccountService bankAccountService;
	private OutlayItemService outlayItemService;
	private LinkmanService linkmanService;
	private CustomerService customerService;
	private ProjectService projectService;

	private Double totalMoney;
	private File upload;
	private String uploadFileName;
	private Collection<BankAccount> bankAccounts;
	private Collection<OutlayItem> outlayItems;
	private Collection<Linkman> linkmans;
	private Collection<Customer> customers;
	private Collection<Project> projects;
	private String bank;
	private OutlayDetail outlayDetail;
	private static Collection<OutlayDetail> outlayDetails;

	static {
		outlayDetails = new ArrayList<OutlayDetail>();
	}

	@Action("listOutlay")
	public String listOutlay() {
		return SUCCESS;
	}

	@Action("listOutlayByBank")
	public String listOutlayByBank() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Outlay.class);
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		if (bank != null) {
			if (!"all".equals(bank)) {
				bank = bank.split(",")[0];
				if (ToolKits.isInteger(bank)) {
					criteria.createAlias("bankAccount", "ba");
					criteria.add(Restrictions.eq("ba.id", Integer
							.parseInt(bank)));
				}
			}
		}
		this.setPagination(this.getOutlayService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editOutlay")
	public String editOutlay() {
		totalMoney = 0.0;
		for (OutlayDetail detail : outlayDetails) {
			totalMoney += detail.getMoney();
		}
		System.out.println("totalMoney:" + totalMoney);
		prepare();
		return SUCCESS;
	}

	@Action("viewOutlay")
	public String viewOutlay() {
		return SUCCESS;
	}

	private void prepare() {
		bankAccounts = this.getBankAccountService().getAllBankAccount();
		outlayItems = this.getOutlayItemService().getAllOutlayItem();
		linkmans = this.getLinkmanService().getAllLinkman();
	}

	@Clew(results = { "success" })
	@Action(value = "submitOutlay", results = {
			@Result(name = "success", type = "redirect", location = "editOutlay"),
			@Result(name = "error", type = "chain", location = "editOutlay") })
	public String submitOutlay() {
		if (outlay.getTitle() == null || "".equals(outlay.getTitle().trim())) {
			addActionError("主题不能为空");
			return ERROR;
		}
		if (outlay.getBankAccount() == null) {
			addActionError("账户不能为空");
			return ERROR;
		}
		if (outlay.getUseTime() == null) {
			addActionError("支出时间不能为空");
			return ERROR;
		}
		if (totalMoney == null) {
			addActionError("总价不能为空");
			return ERROR;
		}
		if (outlay.getUsePerson() == null) {
			addActionError("支出人不能为空");
			return ERROR;
		}
		if (outlay.getIsReturn() == null) {
			addActionError("是否返还不能为空");
			return ERROR;
		}
		if (outlayDetails == null) {
			addActionError("明细不能为空");
			return ERROR;
		}
		totalMoney = 0.0;
		for (OutlayDetail detail : outlayDetails) {
			totalMoney += detail.getMoney();
		}
		outlay.setMoney(totalMoney);
		outlay.setState(Constrains.TASK_AUDITING);
		outlay.setReturnFlag(Constrains.FS_FLAG_NOTRETURN);
		this.getOutlayService().submitOutlay(outlayDetails, outlay, upload,
				uploadFileName);
		outlayDetails.clear();
		this.setPrompt("提交费用单成功");
		return SUCCESS;
	}

	@Action(value = "appendOutlayDetail", results = {
			@Result(name = "success", type = "chain", location = "editOutlay"),
			@Result(name = "error", type = "chain", location = "editOutlay") })
	public String appendOutlayDetail() {
		if (outlayDetail == null || outlayDetail.getMoney() == null) {
			this.addActionError("请输入正确的金额格式");
			return ERROR;
		}
		outlayDetail.setId(outlayDetails.size());
		outlayDetails.add(outlayDetail);

		return SUCCESS;
	}

	@Action(value = "removeOutlayDetail", results = {
			@Result(name = "success", type = "chain", location = "editOutlay"),
			@Result(name = "error", type = "chain", location = "editOutlay") })
	public String removeOutlayDetail() {
		if (ids == null) {
			this.addActionError("请选择要删除的明细");
			return ERROR;
		}

		for (Integer id : ids) {
			Iterator<OutlayDetail> iter = outlayDetails.iterator();
			while (iter.hasNext()) {
				OutlayDetail detial = iter.next();
				if (detial.getId().intValue() == id.intValue()) {
					outlayDetails.remove(detial);
					break;
				}
			}
		}

		return SUCCESS;
	}

	public Outlay getOutlay() {
		return outlay;
	}

	public void setOutlay(Outlay outlay) {
		this.outlay = outlay;
	}

	public OutlayService getOutlayService() {
		return outlayService;
	}

	public void setOutlayService(OutlayService outlayService) {
		this.outlayService = outlayService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setBankAccountService(BankAccountService bankAccountService) {
		this.bankAccountService = bankAccountService;
	}

	public BankAccountService getBankAccountService() {
		return bankAccountService;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setBankAccounts(Collection<BankAccount> bankAccounts) {
		this.bankAccounts = bankAccounts;
	}

	public Collection<BankAccount> getBankAccounts() {
		return bankAccounts;
	}

	public void setOutlayItemService(OutlayItemService outlayItemService) {
		this.outlayItemService = outlayItemService;
	}

	public OutlayItemService getOutlayItemService() {
		return outlayItemService;
	}

	public void setOutlayItems(Collection<OutlayItem> outlayItems) {
		this.outlayItems = outlayItems;
	}

	public Collection<OutlayItem> getOutlayItems() {
		return outlayItems;
	}

	public void setTotalMoney(Double totalMoney) {
		this.totalMoney = totalMoney;
	}

	public Double getTotalMoney() {
		return totalMoney;
	}

	public void setLinkmanService(LinkmanService linkmanService) {
		this.linkmanService = linkmanService;
	}

	public LinkmanService getLinkmanService() {
		return linkmanService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public Collection<Linkman> getLinkmans() {
		return linkmans;
	}

	public void setLinkmans(Collection<Linkman> linkmans) {
		this.linkmans = linkmans;
	}

	public Collection<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(Collection<Customer> customers) {
		this.customers = customers;
	}

	public Collection<Project> getProjects() {
		return projects;
	}

	public void setProjects(Collection<Project> projects) {
		this.projects = projects;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setOutlayDetail(OutlayDetail outlayDetail) {
		this.outlayDetail = outlayDetail;
	}

	public OutlayDetail getOutlayDetail() {
		return outlayDetail;
	}

	public void setOutlayDetails(Collection<OutlayDetail> outlayDetails) {
		this.outlayDetails = outlayDetails;
	}

	public Collection<OutlayDetail> getOutlayDetails() {
		return outlayDetails;
	}

	public String getBank() {
		return bank;
	}

	public void setBank(String bank) {
		this.bank = bank;
	}

}
